﻿using Coldairarrow.Business.Base_Manage;
using Coldairarrow.Entity.Base_leasesystem_car;
using Coldairarrow.Entity.Base_leasesystem_employee;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Base_leasesystem_car
{
    public class leasesystem_carBusiness : BaseBusiness<leasesystem_car>, Ileasesystem_carBusiness, ITransientDependency
    {
        public leasesystem_carBusiness(IDbAccessor db,IHomeBusiness homeBusiness)
            : base(db)
        {
            _homebusiness = homeBusiness;
        }

        IHomeBusiness _homebusiness { get; set; }
        #region 外部接口

        public async Task<PageResult<leasesystem_car>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<leasesystem_car>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<leasesystem_car, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<leasesystem_car> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(leasesystem_car data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(leasesystem_car data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            for (int i = 0; i < ids.Count; i++)
            {
                var entity = GetEntity(ids[i]);
                if (!entity.VehiclelicensePhoto.IsNullOrEmpty()) {
                    string VehiclelicensePhoto = entity.VehiclelicensePhoto.Split('/')[4];
                   await _homebusiness.DeleteFile(Directory.GetCurrentDirectory() + $"\\wwwroot\\Upload\\{VehiclelicensePhoto}");
                }
                if (!entity.BuyCarInvoicePhoto.IsNullOrEmpty()) {
                    string BuyCarInvoicePhoto = entity.BuyCarInvoicePhoto.Split('/')[4];
                   await _homebusiness.DeleteFile(Directory.GetCurrentDirectory() + $"\\wwwroot\\Upload\\{BuyCarInvoicePhoto}");
                }
                if (!entity.CompulsoryinsurancePhoto.IsNullOrEmpty()) {
                    string CompulsoryinsurancePhoto = entity.CompulsoryinsurancePhoto.Split('/')[4];
                   await  _homebusiness.DeleteFile(Directory.GetCurrentDirectory() + $"\\wwwroot\\Upload\\{CompulsoryinsurancePhoto}");
                }
                if (!entity.CommercialinsurancePhoto.IsNullOrEmpty()) {
                    string CommercialinsurancePhoto = entity.CommercialinsurancePhoto.Split('/')[4];
                   await _homebusiness.DeleteFile(Directory.GetCurrentDirectory() + $"\\wwwroot\\Upload\\{CommercialinsurancePhoto}");
                }
                if (!entity.CarfrontPhoto.IsNullOrEmpty())
                {
                    string CarfrontPhoto = entity.CarfrontPhoto.Split('/')[4];
                  await  _homebusiness.DeleteFile(Directory.GetCurrentDirectory() + $"\\wwwroot\\Upload\\{CarfrontPhoto}");
                }
                if (!entity.CarbackPhoto.IsNullOrEmpty())
                {
                    string CarbackPhoto = entity.CarbackPhoto.Split('/')[4];
                    await _homebusiness.DeleteFile(Directory.GetCurrentDirectory() + $"\\wwwroot\\Upload\\{CarbackPhoto}");
                }
                await DeleteAsync(ids[i]);
            }
            
        }

        public async Task<object> GetHeightSearch(PageInput<ConditionDTO> input)
        {
            var q = Db.GetIQueryable<leasesystem_car>();
            var newWhere = q;


            if (!input.Search.Keyword.IsNullOrEmpty())
            {
                newWhere = newWhere.Where(a => a.CarNo.Contains(input.Search.Keyword));
            }
            if (!input.Search.EmpName.IsNullOrEmpty())
            {
                newWhere = newWhere.Where(a => a.EmpName.Contains(input.Search.EmpName));
            }

            if (!input.Search.Status.IsNullOrEmpty())
            {
                newWhere = newWhere.Where(a => a.Status == int.Parse(input.Search.Status));
            }
            if (!input.Search.CarBrandModel.IsNullOrEmpty())
            {
                newWhere = newWhere.Where(a => a.CarBrandModel == input.Search.CarBrandModel);
            }

            var Data = new
            {
                Data = newWhere.Skip((input.PageIndex - 1) * input.PageRows)
               .Take(input.PageRows),
                Total = q.Count()
            };
            return Data;

        }
        #endregion

        #region 私有成员

        #endregion
    }
}